<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Dondathang_model extends CI_Model {
	var $maddh='';
	var $ngaydathang='';
	var $diachi='';
	var $nguoinhan='';
	var $sodienthoai='';	
	var $ghichu='';	
	var $trigia='';	
	var $tinhtrang='';
	var $ma_tk='';	
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
		$this->load->database();
    }
	 
	function insert($data)
    {
       return $this->db->insert('dondathang', $data);
    }
	function getdondathang($maddh)
	{
		$query=$this->db->query("SELECT `maddh`,`nguoinhan`,d.diachi,d.sodienthoai,DATE_FORMAT(ngaydathang,'%d/%m/%y %h:%i:%s') as ngaydathang,`trigia`,`tinhtrang`,`thanhtoan`,`ghichu`,`hoten`,t.diachi as diachigui,t.sodienthoai as sodienthoaigui FROM `dondathang` d  left join `taikhoan` t on d.ma_tk= t.matk  WHERE  `maddh`={$maddh}") ;
       	return $query->row_array();
	}
	function getresult_chitietdonhang($maddh)
	{
		$query=$this->db->query("SELECT `mact`,`tensanpham`,`hinh`,`donvitinh`,d.soluong  FROM `ddh_sp` d  left join `sanpham` s on d.ma_sp= s.masp  WHERE  `ma_ddh`={$maddh}") ;
       	return $query->result_array();
	}
	function getthongtinthanhtoan($maddh)
	{
		$query=$this->db->query("SELECT `matttt`,`tenphuongthuc`,DATE_FORMAT(ngaythanhtoan,'%d/%m/%y %h:%i:%s') as `ngaythanhtoan`,`email`,`magiaodich`,`trigia`	,`tigia`  FROM `thongtinthanhtoan` t  left join `phuongthucthanhtoan` p on t.ma_pt= p.mapt  WHERE  `ma_ddh`={$maddh}") ;
       	return $query->row_array();
	}
	
	function giao($maddh)
	{
		$data=array(
			'tinhtrang'=>'2'
		);
		$this->db->where("maddh",$maddh);
      	return $this->db->update('dondathang', $data);
	}
	function xoa($maddh)
	{
		 $this->db->delete('dondathang', "`maddh` = '".$maddh."'");
	}
	function getresult()
	{
		 $this->db->select("maddh,ma_tk,ngaydathang,diachi,nguoinhan,sodienthoai,ghichu,trigia,tinhtrang,thanhtoan");
         $query=$this->db->get("dondathang");
       	return $query->result_array();
	 }
	 function sanphambannhieu($thang,$nam)
	 {
		$query=$this->db->query("Select * from ( SELECT `masp` , `tensanpham` , s.soluong AS `soluongton`, sum( ddh_sp.soluong ) AS `soluongban`
					FROM `sanpham` s
					LEFT JOIN `ddh_sp` ON s.masp = ddh_sp.ma_sp
					INNER JOIN `dondathang` d ON ddh_sp.ma_ddh = d.maddh
					WHERE d.ngaydathang
					BETWEEN '".$nam."-".$thang."-1'
					AND '".$nam."-".$thang."-31'
					GROUP BY `masp` , `tensanpham` , `soluongton`
					)T 
					ORDER BY T.soluongban DESC 
					LIMIT 0 , 15 ");
		return $query->result_array(); 
	}
	function doanhthu($thang,$nam)
	{
		$query=$this->db->query("Select * From( SELECT `ngaydathang` , sum( trigia ) AS tongtrigia
						FROM `dondathang` d
						WHERE d.ngaydathang
						BETWEEN '".$nam."-".$thang."-1'
						AND '".$nam."-".$thang."-31'
						GROUP BY `ngaydathang`)T ORDER BY T.tongtrigia DESC  ");
		return $query->result_array(); 
	}
	
	
	 function getresult_index()
	{
		$data= $this->input->get();
		$index=1;
		$baitren_mottrang=15;
		
		if(!empty($data['page']))
		{
			$index=$data['page'];
		}else $data['page']=1;
		$query1=null;
		$query2=null;
		if(empty($data['tt']))	
			$data['tt']=0;
				
		// $this->db->select("maddh,ma_tk,ngaydathang,diachi,nguoinhan,sodienthoai,ghichu,trigia,tinhtrang");
		if($data['tt']==0)
		{
			$query1=$this->db->query("SELECT `maddh` FROM `dondathang` WHERE `tinhtrang`='1' or `tinhtrang`='2' ") ;
			
			$query2=$this->db->query("SELECT `maddh`,`nguoinhan`,`diachi`,`sodienthoai`,DATE_FORMAT(ngaydathang,'%d/%m/%y') as ngaydathang,`trigia`,`thanhtoan`,`tinhtrang` FROM `dondathang`  WHERE (`tinhtrang`='1' or `tinhtrang`='2') ORDER BY `ngaydathang` DESC  LIMIT ".(($index-1)*$baitren_mottrang).",".$baitren_mottrang) ;
		}else
		{
			$query1=$this->db->query("SELECT `maddh` FROM `dondathang` WHERE `tinhtrang`={$data['tt']} ") ;
			
			$query2=$this->db->query("SELECT `maddh`,`nguoinhan`,`diachi`,`sodienthoai`,DATE_FORMAT(ngaydathang,'%d/%m/%y') as ngaydathang,`trigia`,`thanhtoan`,`tinhtrang` FROM `dondathang`  WHERE (`tinhtrang`={$data['tt']} )ORDER BY `ngaydathang` DESC  LIMIT ".(($index-1)*$baitren_mottrang).",".$baitren_mottrang) ;
		}
		$db1=$query1->num_rows();
		$db2=$query2->result_array();    
		$data['pages']=$db1/$baitren_mottrang;
		$data['dondathangs']=$db2;
		$data['pagesize']=$baitren_mottrang;
       	return $data;
	 }
	
} 

?>